package com.clkj.zhonghui.controller.product;

import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.pojo.product.*;
import com.clkj.zhonghui.service.product.ProductService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/07/10 10:52:39
 **/
@RestController
@RequestMapping(value = "/product")
@Api(value = "/product", tags = {"商品信息"})
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/addProduct")
    @ApiOperation(value = "添加商品")
    public Result addProduct(Product product, HttpServletRequest request){
        try {

            productService.addProduct(product,request);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @PostMapping("/listProduct")
    @ApiOperation(value = "获取商品列表")
    public Result listProduct(Integer page, Integer size, Integer shopId, String productName, Integer productStatus){
        try {
            PageInfo productList =  productService.listProduct(page,size,shopId,productName,productStatus);
            return new Result(true, StatusCode.OK, "查询成功",productList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/addOrEditFormat")
    @ApiOperation(value = "添加/更新商品规格")
    public Result addOrEditFormat(ProductFormat productFormat,String flag, HttpServletRequest request){
        try {
            productService.addOrEditFormat(productFormat,flag,request);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }

    @PostMapping("/addBannerPic")
    @ApiOperation(value = "添加商品轮播图")
    public Result addBanner(Integer productId,HttpServletRequest request){
        try {
            productService.addBanner(productId,request);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }

    @PostMapping("/addDeailPic")
    @ApiOperation(value = "添加商品详情图")
    public Result addPic(Integer productId,HttpServletRequest request){
        try {
            productService.addDeailPic(productId,request);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }
    @PostMapping("/delOrUpdate")
    @ApiOperation(value = "删除/启用/禁用商品轮播图/详情图")
    public Result delOrUpdate(@ApiParam(name = "picStatus", value = "0为删除 1为启用 2为禁用") @RequestParam("picStatus") Integer  picStatus,
                              @ApiParam(name = "picFlag", value = "0轮播图，1详情图") @RequestParam("picFlag") Integer  picFlag,
                              @ApiParam(name = "ids", value = "需要操作的ID集合字符串") @RequestParam("ids") String  ids){
        try {
            productService.delOrUpdate(ids,picFlag,picStatus);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }

    @PostMapping("/updateProStatus")
    @ApiOperation(value = "更新商品上下架状态")
    public Result updateProStatus(String ids,Integer productStatus){
        try {
            productService.updateProStatus(ids,productStatus);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }

    @PostMapping("/listProductDetail")
    @ApiOperation(value = "获取商品详情")
    public Result listProductDetail(Integer productId){
        try {
            Map result = productService.listProductDetail(productId);
            return new Result(true, StatusCode.OK, "查询成功",result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @PostMapping("/updatepro")
    @ApiOperation(value = "更新商品详情")
    public Result updatepro(Product product,HttpServletRequest request){
        try {
            productService.updatepro(product,request);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }

    @PostMapping("/addSpecial")
    @ApiOperation(value = "添加商品专区")
    public Result addSpecial(ProductSpecials productSpecials,
                             @ApiParam("flag,add为添加，edit 修改") @RequestParam String flag,
                             HttpServletRequest request){
        try {
            productService.addSpecials(productSpecials,flag,request);
            return new Result<>(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/getProductSpecials")
    @ApiOperation(value = "查询商品专区")
    public Result getProductSpecials(@ApiParam("flag,0为查询全部，1为查询上线的") @RequestParam Integer flag){
        try {
            List<ProductSpecials> specialsList = productService.getSpecials(flag);
            return new Result<>(true, StatusCode.OK, "查询成功", specialsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }
    @PostMapping("/getProductByCategory")
    @ApiOperation(value = "根据商品分类查询商品")
    public Result getProductByCategory(@ApiParam("分类ID") @RequestParam Integer categoryId){
        try {
            List<Product> products = productService.getProductByCategory(categoryId);
            return new Result<>(true, StatusCode.OK, "查询成功", products);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/updateSpecial")
    @ApiOperation(value = "更新商品专区状态")
    public Result updateSpecial(String ids ,String status){
        try {
            productService.updateSpecial(ids,status);
            return new Result<>(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "更新失败");
        }
    }

    @PostMapping("/deleteSpecial")
    @ApiOperation(value = "删除商品专区")
    public Result deleteSpecial(String ids){
        try {
            productService.deleteSpecial(ids);
            return new Result<>(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "删除失败");
        }
    }

    @PostMapping("/getComment")
    @ApiOperation(value = "获取商品评论列表")
    public Result getCommen(Integer page,Integer size,Integer productId){
        try {
            PageInfo results = productService.getProductCommentList(page, size, productId);
            return new Result(true, StatusCode.OK, "查询成功",results);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }

    }

    @PostMapping("/updateComment")
    @ApiOperation(value = "商品评论审核通过")
    public Result updateComment(String ids){
        try {
            productService.updateComment(ids);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/importComment")
    @ApiOperation(value = "商品评论导表")
    public Result importComment(HttpServletRequest request){
        try {
            productService.importComment(request);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/deleteProductComm")
    @ApiOperation(value = "删除商品评论")
    public Result deleteProductComm(String ids){
        try {
            productService.deleteProductComm(ids);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }



    @ApiOperation(value = "评论置顶/取消置顶")
    @PostMapping("/zhidingComment")
    public Result zhidingComment(String ids,Integer topping){
        try {
            productService.zhidingComment(ids,topping);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/getAnswer")
    @ApiOperation(value = "获取商品问答")
    public Result getAnswer(Integer page,Integer size,Integer productId,Integer parentId){
        try {
            PageInfo answerList = productService.getAnswer(page,size,productId,parentId);
            return new Result<>(true, StatusCode.OK, "查询成功",answerList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/updateAnswer")
    @ApiOperation(value = "商品问答审核")
    public Result updateAnswer(String ids,
                               @ApiParam(name = "status", value = "要修改之后的状态") @RequestParam("status") Integer status){
        try {
            productService.updateAnswer(ids,status);
            return new Result<>(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/importAnswer")
    @ApiOperation(value = "商品问答导表")
    public Result importAnswer(HttpServletRequest request){
        try {
            productService.importAnswer(request);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/deleteProductAnswer")
    @ApiOperation(value = "删除商品问答")
    public Result deleteProductAnswer(String ids){
        try {
            productService.deleteProductAnswer(ids);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }


    @ApiOperation(value = "app----获取商品专区")
    @PostMapping("/listSpecials")
    public Result listSpecials(){
        try {
            List<ProductSpecials> specials = productService.listSpecials();
            return new Result(true, StatusCode.OK, "操作成功",specials);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @ApiOperation(value = "app----获取商品详情页面")
    @PostMapping("/listProDetail")
    public Result listProDetail(Integer productId,Integer userId){
        try {
            Map map  = productService.listProDetail(productId,userId);
            return new Result(true, StatusCode.OK, "操作成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @ApiOperation(value = "app商品搜索")
    @PostMapping("/getProducts")
    public Result getProducts(Integer page,Integer size,@ApiParam(name = "productName", value = "商品名称") @RequestParam("productName") String productName) {
        try {
            PageInfo products = productService.getProducts(page,size,productName);
            return new Result(true, StatusCode.OK, "查询成功", products);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /*
    * 获取商品问答列表和添加 */

    @PostMapping("/addAnswer")
    @ApiOperation(value = "app-添加问答")
    public Result<Map<String, Object>> addAnswer(ProductAnswer productAnswer) {
        try {
            productService.addAnswer(productAnswer);
            return new Result<>(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "添加失败", "error");
        }
    }

    @PostMapping("/getAnswers")
    @ApiOperation(value = "app-获取商品问答")
    public Result getAnswers(@ApiParam("商品ID") @RequestParam Integer productId) {
        try {
            List<ProductAnswer> answers = productService.getAnswers(productId);
            return new Result<>(true, StatusCode.OK, "获取成功", answers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "获取失败", "error");
        }
    }


    @PostMapping("/getAnswerAndReply")
    @ApiOperation(value = "app-获取商品问答详情")
    public Result getAnswerAndReply(@ApiParam("问题ID") @RequestParam Integer answerId) {
        try {
            ProductAnswer answers = productService.getAnswerAndReply(answerId);
            return new Result<>(true, StatusCode.OK, "获取成功", answers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "获取失败", "error");
        }
    }

    @PostMapping("/getAppComment")
    @ApiOperation(value = "app-获取商品评论列表")
    public Result getAppComment(Integer productId){
        try {
            List<ProductComment> results = productService.getAppComment(productId);
            return new Result(true, StatusCode.OK, "查询成功",results);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }


    @PostMapping("/listCollectProduct")
    @ApiOperation(value = "app--查看商品收藏")
    public Result listCollectProduct(Integer userId){
        try {
            List<CollectProduct> lists = productService.listCollectProduct(userId);
            return new Result<>(true, StatusCode.OK, "查询成功",lists);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/addCollectProduct")
    @ApiOperation(value = "app--收藏商品")
    public Result addCollectProduct(Integer userId,Integer productId,
                                    @ApiParam("flag,0为取消收藏，1为收藏") @RequestParam Integer flag){
        try {
            productService.addCollectProduct(userId,productId,flag);
            return new Result<>(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/delCollectProduct")
    @ApiOperation(value = "app--个人中心取消收藏商品")
    public Result delCollectProduct(String ids){
        try {
            productService.delCollectProduct(ids);
            return new Result<>(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "删除失败");
        }
    }




}
